package web.js;

import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;

import com.example.xuegengwang.xuegengwang.R;
import com.google.gson.Gson;

import java.util.HashMap;

import common.Constant;
import https.InterNetController;
import utils.MyLogUtils;
import utils.ShowShareDialog;
import view.MyConfirmDialog;
import web.bean.ShareBean;

/**
 * Created by Administrator on 2017/3/25.
 * 与js交互的接口
 */

public class CommentInterface {
    //这里需要加@JavascriptInterface，4.2之后提供给javascript调用的函数必须带有@JavascriptInterface
    private Handler handler;
    private static final String TAG = "CommentInterface";
    private String newsid;
    private Context context;
    private WebView college_news_detail_webView;


    private Handler handler1 = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case Constant.ERROR_CODE_SUCCESS://成功返回
                    Gson gson = new Gson();
                    ShareBean shareBean = gson.fromJson((String) msg.obj, ShareBean.class);
                    if (shareBean != null) {
                        ShareBean.NewsParamBean newsParam = shareBean.getNewsParam();
                        if (newsParam != null) {
                            ShowShareDialog showShareDialog = new ShowShareDialog(context, R.style.centerDialog);
                            showShareDialog.setContent(newsParam.getContent());
                            showShareDialog.setImage(newsParam.getImage());
                            showShareDialog.setTitle(newsParam.getTitle());
                            showShareDialog.setUrl(newsParam.getUrl());
                            showShareDialog.show();
                        }
                    }
                    break;
                default:
                    break;
            }
        }
    };


    /***
     * 新闻页android 调用 js 的方法
     */
    private String GOBACK = "javascript:goBack()";//调用web的评论滑出
    private String PUBLISH = "javascript:publish()";//调用web的请求和评论滑出
    private String DELETE_SECOND_COMMENT = "javascript:deleteComment()";//删除二级评论
    private String DELETE_COMMENT = "javascript:deleteAll()";//删除一级评论
    /**
     * 新闻页判断值
     */
    public static final int LOGIN = 3;//登录
    public static final int PERSONAL_PAGE = 4;//跳到个人信息

    public boolean showBtn;
    public static final int SHOWBTN = 5;
    public boolean bindBack;
    public static final int BINDBACK = 6;

    private final int delete_comment = 0;//在handler删除一级评论
    private final int delete_second_comment = 1;//在handler删除二级评论

    private Handler myHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case delete_comment:
                    college_news_detail_webView.loadUrl(DELETE_COMMENT);
                    break;
                case delete_second_comment:
                    college_news_detail_webView.loadUrl(DELETE_SECOND_COMMENT);
                    break;
                default:
                    break;
            }
        }
    };


    public CommentInterface(Context context, Handler handler, WebView college_news_detail_webView, String newsid) {
        this.handler = handler;
        this.newsid = newsid;
        this.context = context;
        this.college_news_detail_webView = college_news_detail_webView;
    }

    /**
     * 新闻页面
     * web调用Android方法
     */


    @JavascriptInterface
    public void share() {
        MyLogUtils.e(TAG, "显示分享弹窗");
        getShareDataFromInterNet("news", newsid);
    }

    @JavascriptInterface
    public void information(String userid) {
        MyLogUtils.e(TAG, "个人信息页面");
        Message msg = new Message();
        msg.what = PERSONAL_PAGE;
        msg.obj = userid;
        handler.sendMessage(msg);
    }

    @JavascriptInterface
    public void logIn() {
        //在页面地址没有ac_id的情况下会出现提示语并且执行此方法跳转到登录页面
        MyLogUtils.e(TAG, "登录");
        Message msg = new Message();
        msg.what = LOGIN;
        handler.sendMessage(msg);
    }


    /**
     * Android调用web方法
     */
    @JavascriptInterface
    public void loginSuccess(String user_id) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("javascript:loginSuccess(");
        stringBuffer.append(user_id);
        stringBuffer.append(")");
        MyLogUtils.e(TAG, "登录成功" + stringBuffer.toString());
        college_news_detail_webView.loadUrl(stringBuffer.toString());
    }

    @JavascriptInterface
    public void loginFailure() {

    }

    @JavascriptInterface
    public void showBtn() {
        showBtn = true;
        MyLogUtils.e(TAG, "显示发布按钮");
        Message msg = new Message();
        msg.obj = showBtn;
        msg.what = SHOWBTN;
        handler.sendMessage(msg);
    }

    @JavascriptInterface
    public void bindBack() {
        bindBack = true;
        MyLogUtils.e(TAG, "按返回时调用web的GoBack()");
        Message msg = new Message();
        msg.obj = bindBack;
        msg.what = BINDBACK;
        handler.sendMessage(msg);
    }


    @JavascriptInterface
    public void unBindBack() {
        bindBack = false;
        MyLogUtils.e(TAG, "按返回时不需要调用goback方法");
        Message msg = new Message();
        msg.obj = bindBack;
        msg.what = BINDBACK;
        handler.sendMessage(msg);
    }


    @JavascriptInterface
    public void hiddenBtn() {
        showBtn = false;
        MyLogUtils.e(TAG, "隐藏发布按钮");
        Message msg = new Message();
        msg.obj = showBtn;
        msg.what = SHOWBTN;
        handler.sendMessage(msg);
    }


    @JavascriptInterface
    public void publish() {
        college_news_detail_webView.loadUrl(PUBLISH);
    }


    @JavascriptInterface
    public void goBack() {
        college_news_detail_webView.loadUrl(GOBACK);
    }

    /**
     * 新闻页面二级评论页
     * web调用Android方法
     */

    @JavascriptInterface
    public void loginFailure(String id) {

    }

    @JavascriptInterface
    public void deleteAll() {
        MyLogUtils.e(TAG, "删除一级评论");
        MyConfirmDialog myConfirmDialog = new MyConfirmDialog(context, "确认删除该条评论?");
        myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
            @Override
            public void confirm() {
                Message message = new Message();
                message.what = delete_comment;
                myHandler.sendMessage(message);
            }
        });
        myConfirmDialog.show();
    }

    @JavascriptInterface
    public void deleteComment() {
        MyLogUtils.e(TAG, "删除二级评论");
        MyConfirmDialog myConfirmDialog = new MyConfirmDialog(context, "确认删除该条评论?");
        myConfirmDialog.setConfirmListener(new MyConfirmDialog.ConfirmListener() {
            @Override
            public void confirm() {
                Message message = new Message();
                message.what = delete_second_comment;
                myHandler.sendMessage(message);
            }
        });
        myConfirmDialog.show();
    }


    private void getShareDataFromInterNet(String type, String newsid) {
        HashMap<String, String> hashMap = new HashMap<>();
        hashMap.put("type", type);
        hashMap.put("newsid", newsid);

        new InterNetController(context, Constant.SHARE, handler1, hashMap, 0);
    }

}
